layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#condition").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });

    $("#ghs").on("input",function(e){
        //获取input输入的值
        Notice.search();
    });
    form.on('select(dm)', function (data) {
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)',function(obj){
        $(obj.tr).siblings().css("background-color","");
        obj.tr.css({"background-color":"#dadad9"});
    });
    /**
     * 检索店名
     */
    //var languageAjax = new $ax(Feng.ctxPath + "/btyBxda/getDm");
    var languageAjax = new $ax(Feng.ctxPath + "/btyBxd/zcDmList");
    var languageResult = languageAjax.start();
    console.info(languageResult);
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');
    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox',hide: true,},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            // {align: 'center', type: "numbers", title: '序号'},
            // {field: 'bh', align: "center", sort: true, title: '编号',width:180},
            {field: 'dm', align: "center", sort: true, title: '店名',fixed: 'left'},
            {field: 'xm', align: "center", title: '姓名',width:140},
            {field: 'ghs', align: "center", title: '供货商名称', width:200},
            // {field: 'sfz', align: "center", sort: true, title: '身份证号',width:180},
            {field: 'yhk', align: "center", sort: true, title: '银行卡号', width:200},
            {field: 'bank', align: "center", title: '银行简称'},
            {field: 'bz', align: "center", sort: true, title: '行号'},
            //{field: 'bank', align: "center", sort: true, title: '开户行'},
            //{field: 'balance', align: "center", sort: true, title: '余额'},
            {field: 'kj', align: "center", sort: true, title: '会计', width:200},
            //{field: 'bz', align: "center", sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth: 220}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['status'] = $("#status").val();
        queryData['dm'] = $("#dm").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 600,
            title: '添加报销档案',
            content: Feng.ctxPath + '/btyBxda/btyBxda_add',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyBxda/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除报销档案 ?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 600,
            title: '修改报销档案',
            content: Feng.ctxPath + "/btyBxda/update_zc?id=" + data.id,
            tableId: Notice.tableId
        });
    };

    /**
     * 点击批量修改会计
     */
    Notice.onEditKj = function (data) {
        func.open({
            height: 600,
            title: '修改会计',
            content: Feng.ctxPath + "/btyBxda/btyBxda_updateKj/" + data.id,
            tableId: Notice.tableId
        });
    };


    /**
     * 点击初始化报销档案
     *
     */
    Notice.btnSbhhNotice = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/btyBxda/initBxda", function (data) {
            if(data === false){
                Feng.error("初始化完成,请勿重复初始化!");
                return;
            }
            Feng.success("初始化完成!");
            table.reload(Notice.tableId);
            location.reload();
        }, function (data) {
            Feng.error("初始化失败!" + data.responseJSON.message + "!");
        });
        ajax.start();
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyBxda/listZc',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        limit: 20,
        skin: 'line',
        cols: Notice.initColumn(),
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });

    // 识别行号按钮点击事件
    $('#btnSbhh').click(function () {
        Notice.btnSbhhNotice();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }else if (layEvent === 'editKj') {
            Notice.onEditKj(data);
        }
    });
});
